using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using Entity;

using System.Data;
namespace DAL
{
    public class SysUserFunDAL
    {
        /// <summary>
        /// 配置单件实例
        /// </summary>
        public static readonly SysUserFunDAL Instance = new SysUserFunDAL();
       
        /// <summary>
        /// 获取菜单
        /// </summary>
        /// <param name="isDir"></param>
        /// <returns></returns>
        public DataTable GetUserFunForLoginName(string loginName)
        {
            DataTable dt = null;
            try
            {
                dt = SqlHelper.ExecuteDataSet("select id,fun_id,user_login_name from sys_user_fun where user_login_name='" + loginName + "'").Tables[0];
            }
            catch (Exception ee)
            {
            }
            return dt;
        }

        /// <summary>
        /// 保存用户对应的菜单
        /// </summary>
        /// <param name="loginName"></param>
        /// <param name="funIds"></param>
        /// <returns></returns>
        public bool SaveUserFun(string loginName, string funIds)
        {
            SqlConnection connection = null;
            SqlCommand command = null;
            SqlTransaction tran = null;
            SqlHelper.ExecuteNonQuery(" delete from sys_user_fun where user_login_name='"+ loginName+"' ", null);
            try
            {
                connection = new SqlConnection(SqlHelper.ConnectionStringLocalTransaction);
                connection.Open();
                tran = connection.BeginTransaction();
                string[] strs = funIds.Split('#');
                for (int i = 0; i < strs.Length; i++)
                {
                    SqlHelper.ExecuteNonQuery(tran, CommandType.Text, " insert into sys_user_fun(fun_id,user_login_name) values ("+strs[i]+",'"+loginName+"') ", null);
                }
                tran.Commit();
            }
            catch (Exception ee)
            {
                tran.Rollback();
                return false;
            }
            finally
            {
                if (connection != null)
                {
                    connection.Close();
                }
            }
            return true;
        }
        
    }
}
